home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / disk / RCS / diskHeader.c,v < prev    next >
Encoding:
Text File  |  1992-12-10  |  59.8 KB  |  2,273 lines

  1. head     1.15;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    eklee:1.15; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.15
  10. date     92.12.09.16.37.08;  author jhh;  state Exp;
  11. branches ;
  12. next     1.14;
  13.  
  14. 1.14
  15. date     92.08.31.12.20.42;  author voelker;  state Exp;
  16. branches ;
  17. next     1.13;
  18.  
  19. 1.13
  20. date     91.10.07.17.39.57;  author voelker;  state Exp;
  21. branches ;
  22. next     1.12;
  23.  
  24. 1.12
  25. date     91.09.23.10.47.33;  author voelker;  state Exp;
  26. branches ;
  27. next     1.11;
  28.  
  29. 1.11
  30. date     91.09.14.15.16.47;  author mendel;  state Exp;
  31. branches ;
  32. next     1.10;
  33.  
  34. 1.10
  35. date     90.03.16.17.41.12;  author jhh;  state Exp;
  36. branches ;
  37. next     1.9;
  38.  
  39. 1.9
  40. date     90.02.16.16.09.23;  author shirriff;  state Exp;
  41. branches ;
  42. next     1.8;
  43.  
  44. 1.8
  45. date     90.01.31.17.05.19;  author jhh;  state Exp;
  46. branches ;
  47. next     1.7;
  48.  
  49. 1.7
  50. date     89.09.25.12.32.34;  author jhh;  state Exp;
  51. branches ;
  52. next     1.6;
  53.  
  54. 1.6
  55. date     89.09.12.11.51.14;  author jhh;  state Exp;
  56. branches ;
  57. next     1.5;
  58.  
  59. 1.5
  60. date     89.07.31.17.42.19;  author jhh;  state Exp;
  61. branches ;
  62. next     1.4;
  63.  
  64. 1.4
  65. date     89.02.09.11.14.19;  author brent;  state Exp;
  66. branches ;
  67. next     1.3;
  68.  
  69. 1.3
  70. date     88.10.27.15.30.43;  author nelson;  state Exp;
  71. branches ;
  72. next     1.2;
  73.  
  74. 1.2
  75. date     88.07.19.11.54.04;  author douglis;  state Exp;
  76. branches ;
  77. next     1.1;
  78.  
  79. 1.1
  80. date     88.06.02.12.54.00;  author brent;  state Exp;
  81. branches ;
  82. next     ;
  83.  
  84.  
  85. desc
  86. @Routines to read the disk header.
  87. @
  88.  
  89.  
  90. 1.15
  91. log
  92. @Added Disk_ReadLfsSegSummary and Disk_WriteLfsSegSummary.
  93. @
  94. text
  95. @/* 
  96.  * diskHeader.c --
  97.  *
  98.  *    Routines to read in the disk header information.
  99.  *
  100.  * Copyright 1990 Regents of the University of California
  101.  * Permission to use, copy, modify, and distribute this
  102.  * software and its documentation for any purpose and without
  103.  * fee is hereby granted, provided that the above copyright
  104.  * notice appear in all copies.  The University of California
  105.  * makes no representations about the suitability of this
  106.  * software for any purpose.  It is provided "as is" without
  107.  * express or implied warranty.
  108.  */
  109.  
  110. #ifndef lint
  111. static char rcsid[] = "$Header: /sprite/src/lib/disk/RCS/diskHeader.c,v 1.14 92/08/31 12:20:42 voelker Exp Locker: jhh $ SPRITE (Berkeley)";
  112. #endif not lint
  113.  
  114. #include "disk.h"
  115. #include <string.h>
  116. #include <stdio.h>
  117. #include <stdlib.h>
  118.  
  119. static Sun_DiskLabel     *ConvertToSunLabel();
  120. static Dec_DiskLabel    *ConvertToDecLabel();
  121. static Disk_Label    *ConvertFromSunLabel();
  122. static Disk_Label    *ConvertFromDecLabel();
  123. static short        SeeSunCheckSum();
  124. static int        CheckSunCheckSum();
  125. static int        MakeSunCheckSum();
  126.  
  127. /*
  128.  * BOOT_SECTOR        Where the boot sectors start on disk.
  129.  */
  130. #define    BOOT_SECTOR        1
  131. #define NUM_BOOT_SECTORS    15
  132.  
  133. /*
  134.  *----------------------------------------------------------------------
  135.  *
  136.  * Disk_ReadDecLabel --
  137.  *
  138.  *    Read the appropriate sector of a disk partition and see if its
  139.  *    a dec label.  If so, return a pointer to a Dec_DiskLabel.
  140.  *    Note:  This reads a Sprite-modified Dec label, which has
  141.  *    additional information.  If the label is a standard dec
  142.  *    label, the numHeads field will be set to -1 to indicate
  143.  *    the Sprite-dependent information is invalid.
  144.  *
  145.  * Results:
  146.  *    A pointer to the label data if could read it, 0 otherwise.
  147.  *
  148.  * Side effects:
  149.  *    Memory allocation.
  150.  *
  151.  *----------------------------------------------------------------------
  152.  */
  153. Dec_DiskLabel *
  154. Disk_ReadDecLabel(fileID)
  155.     int fileID;    /* Handle on raw disk */
  156. {
  157.     Address        buffer;
  158.     Dec_DiskLabel    *decLabelPtr;
  159.  
  160.     buffer = (Address)malloc(DEV_BYTES_PER_SECTOR);
  161.  
  162.     if (Disk_SectorRead(fileID, DEC_LABEL_SECTOR, 1, buffer) < 0) {
  163.     free((char *)buffer);
  164.     return((Dec_DiskLabel *)0);
  165.     } else {
  166.     decLabelPtr = (Dec_DiskLabel *)buffer;
  167.     if (decLabelPtr->magic != DEC_LABEL_MAGIC) {
  168.         free((char*)buffer);
  169.         return((Dec_DiskLabel *)0);
  170.     } else {
  171.         if (decLabelPtr->spriteMagic != FSDM_DISK_MAGIC) {
  172.         /* Original dec label, not Sprite modified dec label. */
  173.         decLabelPtr->numHeads = -1;
  174.         }
  175.         if (decLabelPtr->version != DEC_LABEL_VERSION) {
  176.         printf("Warning: Wrong dec label version: %x vs. %x\n",
  177.             decLabelPtr->version, DEC_LABEL_VERSION);
  178.         }
  179.         return(decLabelPtr);
  180.     }
  181.     }
  182. }
  183.  
  184. /*
  185.  *----------------------------------------------------------------------
  186.  *
  187.  * Disk_ReadSunLabel --
  188.  *
  189.  *    Read the first sector of a disk partition and see if its
  190.  *    a sun label.  If so, return a pointer to a Sun_DiskLabel.
  191.  *
  192.  * Results:
  193.  *    A pointer to the super block data if could read it, 0 otherwise.
  194.  *
  195.  * Side effects:
  196.  *    Memory allocation.
  197.  *
  198.  *----------------------------------------------------------------------
  199.  */
  200. Sun_DiskLabel *
  201. Disk_ReadSunLabel(fileID)
  202.     int fileID;    /* Handle on raw disk */
  203. {
  204.     Address        buffer;
  205.     Sun_DiskLabel    *sunLabelPtr;
  206.  
  207.     buffer = (Address)malloc(DEV_BYTES_PER_SECTOR);
  208.  
  209.     if (Disk_SectorRead(fileID, 0, 1, buffer) < 0) {
  210.     free((char *)buffer);
  211.     return((Sun_DiskLabel *)0);
  212.     } else {
  213.     sunLabelPtr = (Sun_DiskLabel *)buffer;
  214.     if (sunLabelPtr->magic != SUN_DISK_MAGIC) {
  215.         free((char*)buffer);
  216.         return((Sun_DiskLabel *)0);
  217.     } else {
  218.         return(sunLabelPtr);
  219.     }
  220.     }
  221. }
  222.  
  223. /*
  224.  *----------------------------------------------------------------------
  225.  *
  226.  * Disk_ReadDiskHeader --
  227.  *
  228.  *    Read the super block and return a pointer to it.
  229.  *
  230.  * Results:
  231.  *    A pointer to the super block data if could read it, 0 otherwise.
  232.  *
  233.  * Side effects:
  234.  *    Memory allocation.
  235.  *
  236.  *----------------------------------------------------------------------
  237.  */
  238. Fsdm_DiskHeader *
  239. Disk_ReadDiskHeader(openFileID)
  240.     int openFileID;    /* Handle on raw disk */
  241. {
  242.     Address        buffer;
  243.     Fsdm_DiskHeader    *diskHeaderPtr;
  244.  
  245.     buffer = (Address) malloc(DEV_BYTES_PER_SECTOR);
  246.  
  247.     if (Disk_SectorRead(openFileID, 0, 1, buffer) < 0) {
  248.     return((Fsdm_DiskHeader *)0);
  249.     } else {
  250.     diskHeaderPtr = (Fsdm_DiskHeader *)buffer;
  251.     if (diskHeaderPtr->magic != FSDM_DISK_MAGIC) {
  252.         return((Fsdm_DiskHeader *)0);
  253.     } else {
  254.         return(diskHeaderPtr);
  255.     }
  256.     }
  257. }
  258.  
  259. /*
  260.  *----------------------------------------------------------------------
  261.  *
  262.  * Disk_ReadDomainHeader --
  263.  *
  264.  *    Read the domain header and return a pointer to it.
  265.  *
  266.  * Results:
  267.  *    A pointer to the domain header if could read it, NULL otherwise.
  268.  *
  269.  * Side effects:
  270.  *    Memory allocation.
  271.  *
  272.  *----------------------------------------------------------------------
  273.  */
  274. Ofs_DomainHeader *
  275. Disk_ReadDomainHeader(fileID, diskLabelPtr)
  276.     int fileID;            /* Stream to raw disk */
  277.     Disk_Label *diskLabelPtr;    /* Disk label */
  278. {
  279.     Ofs_DomainHeader    *headerPtr;
  280.  
  281.     headerPtr = (Ofs_DomainHeader *)malloc((unsigned) 
  282.     (diskLabelPtr->numDomainSectors * DEV_BYTES_PER_SECTOR));
  283.  
  284.     if (Disk_SectorRead(fileID, diskLabelPtr->domainSector,
  285.                     diskLabelPtr->numDomainSectors,
  286.                     (Address)headerPtr) < 0) {
  287.     return((Ofs_DomainHeader *)0);
  288.     } else {
  289.     if (headerPtr->magic != OFS_DOMAIN_MAGIC) {
  290.         fprintf(stderr, "Disk_ReadDomainHeader, bad magic <%x>\n",
  291.             headerPtr->magic);
  292.         free((Address)headerPtr);
  293.         return((Ofs_DomainHeader *)0);
  294.     } else {
  295.         return(headerPtr);
  296.     }
  297.     }
  298. }
  299.  
  300. /*
  301.  *----------------------------------------------------------------------
  302.  *
  303.  * Disk_WriteDomainHeader --
  304.  *
  305.  *    Write the domain header.
  306.  *
  307.  * Results:
  308.  *    SUCCESS if write succeeded, FAILURE otherwise
  309.  *
  310.  * Side effects:
  311.  *    None.
  312.  *
  313.  *----------------------------------------------------------------------
  314.  */
  315. ReturnStatus
  316. Disk_WriteDomainHeader(fileID, diskLabelPtr, headerPtr)
  317.     int         fileID;            /* Stream to raw disk */
  318.     Disk_Label         *diskLabelPtr;        /* Disk label */
  319.     Ofs_DomainHeader    *headerPtr;        /* Domain header. */
  320. {
  321.  
  322.     return Disk_SectorWrite(fileID, diskLabelPtr->domainSector,
  323.             diskLabelPtr->numDomainSectors, (Address)headerPtr);
  324. }
  325.  
  326. /*
  327.  *----------------------------------------------------------------------
  328.  *
  329.  * Disk_ReadSummaryInfo --
  330.  *
  331.  *    Read the summary information and return a pointer to it.
  332.  *
  333.  * Results:
  334.  *    A pointer to the summary information if it could be read,
  335.  *    NULL otherwise.
  336.  *
  337.  * Side effects:
  338.  *    Memory allocation.
  339.  *
  340.  *----------------------------------------------------------------------
  341.  */
  342. Ofs_SummaryInfo *
  343. Disk_ReadSummaryInfo(fileID, diskLabelPtr)
  344.     int fileID;            /* Stream to raw disk */
  345.     Disk_Label *diskLabelPtr;    /* Disk label */
  346. {
  347.     Ofs_SummaryInfo *summaryPtr;
  348.  
  349.     summaryPtr = (Ofs_SummaryInfo *) malloc (sizeof(Ofs_SummaryInfo));
  350.  
  351.     if (Disk_SectorRead(fileID, diskLabelPtr->summarySector, 
  352.         diskLabelPtr->numSummarySectors, (Address)summaryPtr) < 0) {
  353.     return((Ofs_SummaryInfo *)0);
  354.     } else {
  355.     return(summaryPtr);
  356.     }
  357. }
  358.  
  359. /*
  360.  *----------------------------------------------------------------------
  361.  *
  362.  * Disk_WriteSummaryInfo --
  363.  *
  364.  *    Write the summary information.
  365.  *
  366.  * Results:
  367.  *    SUCCESS if write succeeded, FAILURE otherwise
  368.  *
  369.  * Side effects:
  370.  *    The summary information is written to the disk.
  371.  *
  372.  *----------------------------------------------------------------------
  373.  */
  374. ReturnStatus
  375. Disk_WriteSummaryInfo(fileID, diskLabelPtr, summaryPtr)
  376.     int fileID;            /* Stream to raw disk */
  377.     Disk_Label *diskLabelPtr;    /* Disk label */
  378.     Ofs_SummaryInfo *summaryPtr; /* Summary information */
  379. {
  380.     return Disk_SectorWrite(fileID, diskLabelPtr->summarySector,
  381.         diskLabelPtr->numSummarySectors, (Address)summaryPtr);
  382. }
  383. /*
  384.  *----------------------------------------------------------------------
  385.  *
  386.  * Disk_WriteDecLabel --
  387.  *
  388.  *    Write a DEC label to the appropriate sector of a disk partition.
  389.  *
  390.  * Results:
  391.  *    SUCCESS if write succeeded, FAILURE otherwise
  392.  *
  393.  * Side effects:
  394.  *    The label is written to the disk.
  395.  *
  396.  *----------------------------------------------------------------------
  397.  */
  398. ReturnStatus
  399. Disk_WriteDecLabel(fileID, labelPtr)
  400.     int         fileID;        /* Handle on raw disk */
  401.     Dec_DiskLabel     *labelPtr;     /* Ptr to DEC label */
  402. {
  403.     return Disk_SectorWrite(fileID, DEC_LABEL_SECTOR, 1, (Address) labelPtr);
  404. }
  405.  
  406. /*
  407.  *----------------------------------------------------------------------
  408.  *
  409.  * Disk_WriteSunLabel --
  410.  *
  411.  *    Write a Sun label to the appropriate sector of a disk partiton.
  412.  *
  413.  * Results:
  414.  *    0 if write succeeded, -1 otherwise
  415.  *
  416.  * Side effects:
  417.  *    The label is written to the disk.
  418.  *
  419.  *----------------------------------------------------------------------
  420.  */
  421. int
  422. Disk_WriteSunLabel(fileID, labelPtr)
  423.     int         fileID;        /* Handle on raw disk */
  424.     Sun_DiskLabel    *labelPtr;    /* Ptr to Sun label */
  425. {
  426.     return Disk_SectorWrite(fileID, 0, 1, (Address) labelPtr);
  427. }
  428.  
  429. /*
  430.  *----------------------------------------------------------------------
  431.  *
  432.  * Disk_ReadLabel --
  433.  *
  434.  *    Read a label off the disk and convert it to a Disk_Label.
  435.  *
  436.  * Results:
  437.  *    Pointer to Disk_Label if the label was read and converted,
  438.  *    NULL otherwise.
  439.  *
  440.  * Side effects:
  441.  *    Memory allocation.
  442.  *
  443.  *----------------------------------------------------------------------
  444.  */
  445.  
  446. Disk_Label *
  447. Disk_ReadLabel(fileID)
  448.     int fileID;            /* Handle on raw disk */
  449. {
  450.     Sun_DiskLabel    *sunLabelPtr;
  451.     Dec_DiskLabel    *decLabelPtr;
  452.  
  453.     sunLabelPtr = Disk_ReadSunLabel(fileID);
  454.     if (sunLabelPtr != (Sun_DiskLabel *)0) {
  455.     return ConvertFromSunLabel(fileID, sunLabelPtr);
  456.     } 
  457.     decLabelPtr = Disk_ReadDecLabel(fileID);
  458.     if (decLabelPtr != (Dec_DiskLabel *)0) {
  459.     return ConvertFromDecLabel(fileID, decLabelPtr);
  460.     }
  461.     return NULL;
  462. }
  463.  
  464. /*
  465.  *----------------------------------------------------------------------
  466.  *
  467.  * Disk_WriteLabel --
  468.  *
  469.  *    Converts a Disk_Label into a machine-specific disk label and
  470.  *    writes it on the disk.
  471.  *
  472.  * Results:
  473.  *    0 if everything worked
  474.  *    -1 if the label could not be converted
  475.  *    -2 otherwise
  476.  *
  477.  * Side effects:
  478.  *    A label is written on the disk.
  479.  *    Memory allocation.
  480.  *
  481.  *----------------------------------------------------------------------
  482.  */
  483.  
  484. int
  485. Disk_WriteLabel(fileID, labelPtr)
  486.     int         fileID;        /* Handle on raw disk */
  487.     Disk_Label        *labelPtr;     /* Ptr to label to write. */
  488. {
  489.     int            status;    
  490.  
  491.     switch(labelPtr->labelType) {
  492.     case DISK_SUN_LABEL: {
  493.         Sun_DiskLabel    *sunLabelPtr;
  494.         sunLabelPtr = ConvertToSunLabel(labelPtr);
  495.         if (sunLabelPtr == NULL) {
  496.         return -1;
  497.         }
  498.         status = Disk_WriteSunLabel(fileID, sunLabelPtr);
  499.         free((char *) sunLabelPtr);
  500.         if (status < 0) {
  501.         return -2;
  502.         }
  503.         break;
  504.     }
  505.     case DISK_DEC_LABEL: {
  506.         Dec_DiskLabel    *decLabelPtr;
  507.         decLabelPtr = ConvertToDecLabel(labelPtr);
  508.         if (decLabelPtr == NULL) {
  509.         return -1;
  510.         }
  511.         status = Disk_WriteDecLabel(fileID, decLabelPtr);
  512.         free((char *) decLabelPtr);
  513.         if (status < 0) {
  514.         return -2;
  515.         }
  516.         break;
  517.     }
  518.     default :
  519.         fprintf(stderr, "Unknown label type %d\n", labelPtr->labelType);
  520.         return -1;
  521.         break;
  522.     }
  523.     return 0;
  524. }
  525.  
  526. /*
  527.  *----------------------------------------------------------------------
  528.  *
  529.  * Disk_NewLabel --
  530.  *
  531.  *    Creates a new disk label.
  532.  *
  533.  * Results:
  534.  *    NULL if there was an error, a new disk label otherwise.
  535.  *
  536.  * Side effects:
  537.  *    Memory allocation
  538.  *
  539.  *----------------------------------------------------------------------
  540.  */
  541.  
  542. Disk_Label *
  543. Disk_NewLabel(labelType) 
  544.     Disk_NativeLabelType    labelType;    /* Native type of label. */
  545. {
  546.     Disk_Label        *labelPtr;
  547.  
  548.     labelPtr = (Disk_Label *) malloc(sizeof(Disk_Label));
  549.     if (labelPtr == NULL) {
  550.     fprintf(stderr, "Disk_NewLabel: Out of memory.\n");
  551.     return NULL;
  552.     }
  553.     bzero((char *) labelPtr, sizeof(Disk_Label));
  554.     strcpy(labelPtr->asciiLabel, "New label");
  555.     switch(labelType) {
  556.     case DISK_SUN_LABEL: 
  557.         labelPtr->asciiLabelLen = 128;
  558.         labelPtr->numPartitions = SUN_NUM_DISK_PARTS;
  559.         labelPtr->bootSector = SUN_BOOT_SECTOR;
  560.         labelPtr->numBootSectors = SUN_SUMMARY_SECTOR - SUN_BOOT_SECTOR -1;
  561.         labelPtr->summarySector = SUN_SUMMARY_SECTOR;
  562.         labelPtr->numSummarySectors = 1;
  563.         labelPtr->domainSector = SUN_DOMAIN_SECTOR;
  564.         labelPtr->numDomainSectors = OFS_NUM_DOMAIN_SECTORS;
  565.         labelPtr->labelType = labelType;
  566.         labelPtr->labelPtr = NULL;
  567.         labelPtr->labelSector = SUN_LABEL_SECTOR;
  568.         break;
  569.     case DISK_DEC_LABEL: 
  570.         labelPtr->asciiLabelLen = 128;
  571.         labelPtr->numPartitions = DEC_NUM_DISK_PARTS;
  572.         labelPtr->bootSector = DEC_BOOT_SECTOR;
  573.         labelPtr->numBootSectors = DEC_SUMMARY_SECTOR - DEC_BOOT_SECTOR -1;
  574.         labelPtr->summarySector = DEC_SUMMARY_SECTOR;
  575.         labelPtr->numSummarySectors = 1;
  576.         labelPtr->domainSector = DEC_DOMAIN_SECTOR;
  577.         labelPtr->numDomainSectors = OFS_NUM_DOMAIN_SECTORS;
  578.         labelPtr->labelType = labelType;
  579.         labelPtr->labelPtr = NULL;
  580.         labelPtr->labelSector = DEC_LABEL_SECTOR;
  581.         break;
  582.     default : 
  583.         fprintf(stderr, "Unknown label type %d\n", labelType);
  584.         free((char *) labelPtr);
  585.         labelPtr = NULL;
  586.         break;
  587.     }
  588.     return labelPtr;
  589. }
  590.  
  591.  
  592.  
  593. /*
  594.  *----------------------------------------------------------------------
  595.  *
  596.  * ConvertFromSunLabel --
  597.  *
  598.  *    Makes a Disk_Label from a Sun_DiskLabel
  599.  *
  600.  * Results:
  601.  *    Pointer to Disk_Label if the label was read and converted,
  602.  *    NULL otherwise.
  603.  *
  604.  * Side effects:
  605.  *    Memory allocation
  606.  *
  607.  *----------------------------------------------------------------------
  608.  */
  609.  
  610. static Disk_Label *
  611. ConvertFromSunLabel(fileID, sunLabelPtr)
  612.     int         fileID;        /* Handle on raw disk */
  613.     Sun_DiskLabel    *sunLabelPtr;    /* Sun label to be converted */
  614. {
  615.     Disk_Label        *labelPtr;
  616.     int            i;
  617.     char         buffer[DEV_BYTES_PER_SECTOR * OFS_NUM_DOMAIN_SECTORS];
  618.     Ofs_DomainHeader    *domainHeaderPtr = (Ofs_DomainHeader *) buffer;
  619.     int            sectorsPerCyl;
  620.  
  621.     labelPtr = (Disk_Label *) malloc(sizeof(Disk_Label));
  622.     if (labelPtr == NULL) {
  623.     fprintf(stderr, "Malloc failed.\n");
  624.     return NULL;
  625.     }
  626.     bzero((char *) labelPtr, sizeof(Disk_Label));
  627.     if (sunLabelPtr->magic != SUN_DISK_MAGIC) {
  628.     fprintf(stderr, "Bad magic number on disk <%x> not <%x>\n",
  629.         sunLabelPtr->magic, SUN_DISK_MAGIC);
  630.     }
  631.     if (!CheckSunCheckSum(sunLabelPtr)) {
  632.     printf("Check sum incorrect, 0x%x not 0x%x\n",
  633.         SeeSunCheckSum(sunLabelPtr), sunLabelPtr->checkSum);
  634.     }
  635.     labelPtr->numCylinders = sunLabelPtr->numCylinders;
  636.     labelPtr->numAltCylinders = sunLabelPtr->numAltCylinders;
  637.     labelPtr->numHeads = sunLabelPtr->numHeads;
  638.     labelPtr->numSectors = sunLabelPtr->numSectors;
  639.     labelPtr->asciiLabelLen = 128;
  640.     strncpy(labelPtr->asciiLabel, sunLabelPtr->asciiLabel, 128);
  641.     labelPtr->bootSector = BOOT_SECTOR;
  642.     labelPtr->numDomainSectors = OFS_NUM_DOMAIN_SECTORS;
  643.     /*
  644.      * Go looking for the domain header.  This allows us a variable
  645.      * number of boot sectors, without having to add additional fields
  646.      * to the Sun label.
  647.      */
  648.     for (i = BOOT_SECTOR + 1; 
  649.      i < FSDM_MAX_BOOT_SECTORS + 3; 
  650.      i+= FSDM_BOOT_SECTOR_INC) {
  651.     if (Disk_SectorRead(fileID, i, OFS_NUM_DOMAIN_SECTORS, buffer) < 0) {
  652.         fprintf(stderr, "Can't read sector %d.\n", i);
  653.         return(NULL);
  654.     }
  655.     if (domainHeaderPtr->magic == OFS_DOMAIN_MAGIC) {
  656.         labelPtr->summarySector = i - 1;
  657.         labelPtr->domainSector = i;
  658.         labelPtr->numBootSectors = labelPtr->summarySector - 1;
  659.         break;
  660.     }
  661.     }
  662.     /* 
  663.      * Check if we found a domain header.
  664.      */
  665.     if (i >= FSDM_MAX_BOOT_SECTORS + 3) {
  666.     labelPtr->summarySector = -1;
  667.     labelPtr->domainSector = -1;
  668.     labelPtr->numBootSectors = -1;
  669.     }
  670.     labelPtr->numSummarySectors = 1;
  671.     labelPtr->numPartitions = SUN_NUM_DISK_PARTS;
  672.     sectorsPerCyl = labelPtr->numHeads * labelPtr->numSectors;
  673.     for (i = 0; i < labelPtr->numPartitions; i++) {
  674.     labelPtr->partitions[i].firstCylinder = sunLabelPtr->map[i].cylinder;
  675.     if (sunLabelPtr->map[i].numBlocks % sectorsPerCyl != 0) {
  676.         printf(
  677.     "Warning: size of partition %d (0x%x) is not multiple of cylinder size\n",
  678.             i, sunLabelPtr->map[i].numBlocks);
  679.     }
  680.     labelPtr->partitions[i].numCylinders =     
  681.         sunLabelPtr->map[i].numBlocks / sectorsPerCyl;
  682.     }
  683.     labelPtr->labelSector = SUN_LABEL_SECTOR;
  684.     labelPtr->labelType = DISK_SUN_LABEL;
  685.     labelPtr->labelPtr = (char *) sunLabelPtr;
  686.     return labelPtr;
  687. }
  688. /*
  689.  *----------------------------------------------------------------------
  690.  *
  691.  * ConvertFromDecLabel --
  692.  *
  693.  *    Makes a Disk_Label from a Dec_DiskLabel
  694.  *
  695.  * Results:
  696.  *    Pointer to Disk_Label if the label was read and converted,
  697.  *    NULL otherwise.
  698.  *
  699.  * Side effects:
  700.  *    Memory allocation
  701.  *
  702.  *----------------------------------------------------------------------
  703.  */
  704. /*ARGSUSED*/
  705. static Disk_Label *
  706. ConvertFromDecLabel(fileID, decLabelPtr)
  707.     int         fileID;        /* Handle on raw disk */
  708.     Dec_DiskLabel    *decLabelPtr;    /* DEC label to be converted */
  709. {
  710.     Disk_Label        *labelPtr;
  711.     int            bytesPerCyl;
  712.     int            i;
  713.  
  714.     if (decLabelPtr->numHeads == -1) {
  715.     fprintf(stderr, "Dec label does not have Sprite extensions.\n");
  716.     return NULL;
  717.     }
  718.     labelPtr = (Disk_Label *) malloc(sizeof(Disk_Label));
  719.     if (labelPtr == NULL) {
  720.     fprintf(stderr, "Malloc failed.\n");
  721.     return NULL;
  722.     }
  723.     bzero((char *) labelPtr, sizeof(Disk_Label));
  724.     if (decLabelPtr->magic != DEC_LABEL_MAGIC) {
  725.     printf("Bad magic number on disk <%x> not <%x>\n",
  726.         decLabelPtr->magic, DEC_LABEL_MAGIC);
  727.     }
  728.     labelPtr->numSectors = decLabelPtr->numSectors;
  729.     labelPtr->numHeads = decLabelPtr->numHeads;
  730.     labelPtr->numCylinders = decLabelPtr->numCylinders;
  731.     labelPtr->numAltCylinders = decLabelPtr->numAltCylinders;
  732.     labelPtr->asciiLabelLen = 128;
  733.     strncpy(labelPtr->asciiLabel, decLabelPtr->asciiLabel, 128);
  734.     labelPtr->bootSector = decLabelPtr->bootSector;
  735.     labelPtr->numDomainSectors = decLabelPtr->numDomainSectors;
  736.     labelPtr->numBootSectors = decLabelPtr->numBootSectors;
  737.     labelPtr->summarySector = decLabelPtr->summarySector;
  738.     labelPtr->domainSector = decLabelPtr->domainSector;
  739.     labelPtr->numDomainSectors = decLabelPtr->numDomainSectors;
  740.     labelPtr->numSummarySectors = 1;
  741.     labelPtr->numPartitions = DEC_NUM_DISK_PARTS;
  742.     bytesPerCyl = labelPtr->numHeads * labelPtr->numSectors *
  743.         DEV_BYTES_PER_SECTOR;
  744.     for (i = 0; i < labelPtr->numPartitions; i++) {
  745.     if (decLabelPtr->map[i].offsetBytes % bytesPerCyl != 0) {
  746.         printf(
  747.     "Warning: start of partition %d (0x%x) is not multiple of cylinder size\n",
  748.             i, decLabelPtr->map[i].offsetBytes);
  749.     }
  750.     labelPtr->partitions[i].firstCylinder = 
  751.         decLabelPtr->map[i].offsetBytes / bytesPerCyl;
  752.     if (decLabelPtr->map[i].numBytes % bytesPerCyl != 0) {
  753.         printf(
  754.     "Warning: size of partition %d (0x%x) is not multiple of cylinder size\n",
  755.             i, decLabelPtr->map[i].numBytes);
  756.     }
  757.     labelPtr->partitions[i].numCylinders = 
  758.         decLabelPtr->map[i].numBytes / bytesPerCyl;
  759.     }
  760.     labelPtr->labelSector = DEC_LABEL_SECTOR;
  761.     labelPtr->labelType = DISK_DEC_LABEL;
  762.     labelPtr->labelPtr = (char *) decLabelPtr;
  763.     return labelPtr;
  764. }
  765.  
  766. /*
  767.  *----------------------------------------------------------------------
  768.  *
  769.  *  ConvertToSunLabel --
  770.  *
  771.  *    Makes a Sun_DiskLabel from a Disk_Label
  772.  *
  773.  * Results:
  774.  *    Pointer to Sun_DiskLabel if the label was converted and written,
  775.  *    NULL otherwise.
  776.  *
  777.  * Side effects:
  778.  *    Memory allocation
  779.  *
  780.  *----------------------------------------------------------------------
  781.  */
  782.  
  783. static Sun_DiskLabel *
  784. ConvertToSunLabel(labelPtr)
  785.     Disk_Label        *labelPtr;    /* Label to be converted */
  786. {
  787.     Sun_DiskLabel    *sunLabelPtr;
  788.     int            i;
  789.  
  790.     if (labelPtr->numPartitions > SUN_NUM_DISK_PARTS) {
  791.     fprintf(stderr, "Too many disk partitions for a Sun label (%d > %d)\n",
  792.         labelPtr->numPartitions, SUN_NUM_DISK_PARTS);
  793.     return NULL;
  794.     }
  795.     sunLabelPtr = (Sun_DiskLabel *) malloc(sizeof(Sun_DiskLabel));
  796.     if (sunLabelPtr == NULL) {
  797.     fprintf(stderr, "Malloc failed.\n");
  798.     return NULL;
  799.     }
  800.     bzero((char *) sunLabelPtr, sizeof(Sun_DiskLabel));
  801.     sunLabelPtr->magic = SUN_DISK_MAGIC;
  802.     sunLabelPtr->numCylinders = labelPtr->numCylinders;
  803.     sunLabelPtr->numAltCylinders = labelPtr->numAltCylinders;
  804.     sunLabelPtr->numHeads = labelPtr->numHeads;
  805.     sunLabelPtr->numSectors = labelPtr->numSectors;
  806.     strncpy(sunLabelPtr->asciiLabel, labelPtr->asciiLabel, 128);
  807.     sunLabelPtr->partitionID = 0;
  808.     sunLabelPtr->bhead = 0;
  809.     sunLabelPtr->gap1 = 65535;
  810.     sunLabelPtr->gap2 = 65535;
  811.     sunLabelPtr->interleave = 1;
  812.     for (i = 0; i < SUN_NUM_DISK_PARTS; i++) {
  813.     sunLabelPtr->map[i].cylinder = labelPtr->partitions[i].firstCylinder;
  814.     sunLabelPtr->map[i].numBlocks = labelPtr->partitions[i].numCylinders *
  815.         labelPtr->numHeads * labelPtr->numSectors;
  816.     }
  817.     MakeSunCheckSum(sunLabelPtr);
  818.     return sunLabelPtr;
  819. }
  820. /*
  821.  *----------------------------------------------------------------------
  822.  *
  823.  * ConvertToDecLabel --
  824.  *
  825.  *    Makes a Dec_DiskLabel from a Disk_Label
  826.  *
  827.  * Results:
  828.  *    Pointer to Dec_DiskLabel if the label was converted and written,
  829.  *    NULL otherwise.
  830.  *
  831.  * Side effects:
  832.  *    Memory allocation
  833.  *
  834.  *----------------------------------------------------------------------
  835.  */
  836. static Dec_DiskLabel *
  837. ConvertToDecLabel(labelPtr)
  838.     Disk_Label    *labelPtr;    /* Label to be converted */
  839. {
  840.     Dec_DiskLabel    *decLabelPtr;
  841.     int            bytesPerCyl;
  842.     int            i;
  843.  
  844.     if (labelPtr->numPartitions > DEC_NUM_DISK_PARTS) {
  845.     fprintf(stderr, "Too many disk partitions for a DEC label (%d > %d)\n",
  846.         labelPtr->numPartitions, DEC_NUM_DISK_PARTS);
  847.     return NULL;
  848.     }
  849.     decLabelPtr = (Dec_DiskLabel *) malloc(sizeof(Dec_DiskLabel));
  850.     if (decLabelPtr == NULL) {
  851.     fprintf(stderr, "Malloc failed.\n");
  852.     return NULL;
  853.     }
  854.     bzero((char *) decLabelPtr, sizeof(Dec_DiskLabel));
  855.     decLabelPtr->magic = DEC_LABEL_MAGIC;
  856.     decLabelPtr->spriteMagic = FSDM_DISK_MAGIC;
  857.     decLabelPtr->version = DEC_LABEL_VERSION;
  858.     decLabelPtr->isPartitioned = 1;
  859.     decLabelPtr->numHeads = labelPtr->numHeads;
  860.     decLabelPtr->numSectors = labelPtr->numSectors;
  861.     decLabelPtr->domainSector = labelPtr->domainSector;
  862.     decLabelPtr->numDomainSectors = labelPtr->numDomainSectors;
  863.     decLabelPtr->numCylinders = labelPtr->numCylinders;
  864.     decLabelPtr->numAltCylinders = labelPtr->numAltCylinders;
  865.     decLabelPtr->bootSector = labelPtr->bootSector;
  866.     decLabelPtr->numBootSectors = labelPtr->numBootSectors;
  867.     decLabelPtr->summarySector = labelPtr->summarySector;
  868.     strncpy(decLabelPtr->asciiLabel, labelPtr->asciiLabel, 128);
  869.     bytesPerCyl = labelPtr->numHeads *labelPtr->numSectors *
  870.         DEV_BYTES_PER_SECTOR;
  871.     for (i = 0; i < DEC_NUM_DISK_PARTS; i++) {
  872.     decLabelPtr->map[i].numBytes = 
  873.         labelPtr->partitions[i].numCylinders * bytesPerCyl;
  874.     decLabelPtr->map[i].offsetBytes = 
  875.         labelPtr->partitions[i].firstCylinder * bytesPerCyl;
  876.     }
  877.     return decLabelPtr;
  878. }
  879.  
  880. /*
  881.  *----------------------------------------------------------------------
  882.  *
  883.  * Sun checksum routines --
  884.  *
  885.  *    The following routines manipulate the checksum in a Sun label.
  886.  *
  887.  * Results:
  888.  *    None.
  889.  *
  890.  * Side effects:
  891.  *    None.
  892.  *
  893.  *----------------------------------------------------------------------
  894.  */
  895.  
  896. static short
  897. SeeSunCheckSum(sunLabelPtr)
  898.     Sun_DiskLabel *sunLabelPtr;
  899. {
  900.         short *sp, sum = 0;
  901.         short count = DEV_BYTES_PER_SECTOR/sizeof(short) - 1;
  902.  
  903.         sp = (short *)sunLabelPtr;
  904.         while (count--)
  905.                 sum ^= *sp++;
  906.         return (sum);
  907. }
  908.  
  909. static int
  910. CheckSunCheckSum(sunLabelPtr)
  911.     Sun_DiskLabel *sunLabelPtr;
  912. {
  913.         short *sp, sum = 0;
  914.         short count = DEV_BYTES_PER_SECTOR/sizeof(short);
  915.  
  916.         sp = (short *)sunLabelPtr;
  917.         while (count--)
  918.                 sum ^= *sp++;
  919.         return (sum ? 0 : 1);
  920. }
  921.  
  922. static int
  923. MakeSunCheckSum(sunLabelPtr)
  924.     Sun_DiskLabel *sunLabelPtr;
  925. {
  926.         short *sp, sum = 0;
  927.         short count = DEV_BYTES_PER_SECTOR/sizeof(short) - 1;
  928.  
  929.         sunLabelPtr->checkSum = 0;
  930.         sp = (short *)sunLabelPtr;
  931.         while (count--)
  932.                 sum ^= *sp++;
  933.         sunLabelPtr->checkSum = sum;
  934. }
  935.  
  936. /*
  937.  *----------------------------------------------------------------------
  938.  *
  939.  * Disk_EraseLabel --
  940.  *
  941.  *    Erase a label from the disk.
  942.  *
  943.  * Results:
  944.  *    0 if the label was erased properly
  945.  *    -1 otherwise.
  946.  *
  947.  * Side effects:
  948.  *    A sector is cleared on the disk.
  949.  *
  950.  *----------------------------------------------------------------------
  951.  */
  952.  
  953. int
  954. Disk_EraseLabel(fileID, labelType)
  955.     int                fileID;        /* Handle on raw disk. */
  956.     Disk_NativeLabelType    labelType;    /* Type of label. */
  957. {
  958.     char    buffer[DEV_BYTES_PER_SECTOR];
  959.     int        sector;
  960.  
  961.     switch (labelType) {
  962.     case DISK_SUN_LABEL:
  963.         sector = SUN_LABEL_SECTOR;
  964.         break;
  965.     case DISK_DEC_LABEL:
  966.         sector = DEC_LABEL_SECTOR;
  967.         break;
  968.     default:
  969.         printf("Unknown label type.\n");
  970.         return -1;
  971.     }
  972.     bzero(buffer, DEV_BYTES_PER_SECTOR);
  973.     return Disk_SectorWrite(fileID, sector, 1, buffer);
  974. }
  975.  
  976. /*
  977.  *----------------------------------------------------------------------
  978.  *
  979.  * Disk_HasFilesystem
  980.  *
  981.  *    Determines if a filesystem can be found on the disk stream.
  982.  *
  983.  * Results:
  984.  *    DISK_HAS_NO_FS if no filesystem could be found;
  985.  *      DISK_HAS_OFS if an old filesystem was found;
  986.  *      DISK_HAS_LFS if a log structured filesystem was found;
  987.  *
  988.  * Side effects:
  989.  *    Memory allocation.
  990.  *
  991.  *----------------------------------------------------------------------
  992.  */
  993. int
  994. Disk_HasFilesystem(fileID, diskLabelPtr)
  995.     int fileID;                /* Disk stream */
  996.     Disk_Label *diskLabelPtr;  /* Disk Label */ 
  997. {
  998.     Ofs_DomainHeader *headerPtr;
  999.     LfsSuperBlock    *superPtr;
  1000.     int              status;
  1001.     unsigned int     magic;
  1002.  
  1003.     /*
  1004.      * Note: We check for an OFS first because when an OFS is created
  1005.      *       the old filesystem on the disk is not explicitly erased.
  1006.      *       So if the old filesystem was a LFS, is is possible that
  1007.      *       the LfsSuperBlock is still on the disk.  'fsmake' should
  1008.      *       be changed to erase the old filesystem, but since the OFS
  1009.      *       is rarely used it is not crucial.  'mklfs' does the
  1010.      *       right thing.
  1011.      *
  1012.      *       Also, Disk_ReadDomainHeader() and Disk_ReadLfsSuperBlock()
  1013.      *       were not used because we don't want any error messages
  1014.      *       printed out.
  1015.      */
  1016.  
  1017.     /*
  1018.      * check for an OFS by trying to read a domain header.  If one
  1019.      * is found that has a valid magic number, then assume that
  1020.      * an OFS is on the disk
  1021.      */
  1022.     headerPtr = (Ofs_DomainHeader *)malloc((unsigned) 
  1023.     (diskLabelPtr->numDomainSectors * DEV_BYTES_PER_SECTOR));
  1024.     if (headerPtr == NULL) {
  1025.     perror("Disk_HasFilesystem:  allocating Ofs_DomainHeader");
  1026.     exit(FAILURE);
  1027.     }
  1028.     /*
  1029.      * if the domainSector is negative, then there is no Ofs_DomainHeader
  1030.      * on the disk -- just check to see if it is an LFS then...
  1031.      */
  1032.     if (diskLabelPtr -> domainSector >= 0 && 
  1033.     diskLabelPtr -> summarySector >= 0 ) {
  1034.     status = Disk_SectorRead(fileID, diskLabelPtr -> domainSector,
  1035.                  diskLabelPtr -> numDomainSectors,
  1036.                  (Address)headerPtr);
  1037.     magic = headerPtr -> magic;
  1038.     free(headerPtr);
  1039.     } else {
  1040.     status = -1;
  1041.     }
  1042.     if (status < 0 || magic != OFS_DOMAIN_MAGIC) {
  1043.     /*
  1044.      * check for a LFS by trying to read a LfsSuperBlock.  If
  1045.      * one is found that has a valid magic number, then assume that
  1046.      * a LFS is on the disk
  1047.      */
  1048.     superPtr = (LfsSuperBlock *)malloc(LFS_SUPER_BLOCK_SIZE);
  1049.     if (superPtr == NULL) {
  1050.         perror("Disk_HasFilesystem:  allocating LfsSuperBlock");
  1051.         exit(FAILURE);
  1052.     }
  1053.     status = Disk_SectorRead(fileID, LFS_SUPER_BLOCK_OFFSET,
  1054.                  LFS_SUPER_BLOCK_SIZE / DEV_BYTES_PER_SECTOR,
  1055.                  (Address)superPtr);
  1056.     magic = superPtr ->hdr.magic;
  1057.     free(superPtr);
  1058.     if (status < 0 || magic != LFS_SUPER_BLOCK_MAGIC) {
  1059.         return DISK_HAS_NO_FS;
  1060.     } else {
  1061.         return DISK_HAS_LFS;
  1062.     }
  1063.     } else {
  1064.     return DISK_HAS_OFS;
  1065.     }
  1066. }
  1067.  
  1068. /*
  1069.  *----------------------------------------------------------------------
  1070.  *
  1071.  * Disk_ReadLfsSuperBlock
  1072.  *
  1073.  *    Read the LFS SuperBlock from the stream.
  1074.  *
  1075.  * Results:
  1076.  *    A pointer to the LFS SuperBlock if could read it, NULL otherwise.
  1077.  *
  1078.  * Side effects:
  1079.  *    Memory allocation.
  1080.  *
  1081.  *----------------------------------------------------------------------
  1082.  */
  1083. LfsSuperBlock *
  1084. Disk_ReadLfsSuperBlock(fileID, diskLabelPtr)
  1085.     int fileID;                /* Disk stream */
  1086.     Disk_Label *diskLabelPtr;  /* Disk Label */
  1087. {
  1088.     LfsSuperBlock    *superPtr;
  1089.     int              fstype, status;
  1090.     
  1091.     fstype = Disk_HasFilesystem(fileID, diskLabelPtr);
  1092.     if (fstype != DISK_HAS_LFS) {
  1093.     return ((LfsSuperBlock *)NULL);
  1094.     }
  1095.     /*
  1096.      * Note:  Reading of LfsSuperBlock is dependent on the blockSize constant
  1097.      * set at the top of /sprite/src/admin/mklfs/mklfs.c
  1098.      */
  1099.     superPtr = (LfsSuperBlock *)malloc(LFS_SUPER_BLOCK_SIZE);
  1100.     if (superPtr == NULL) {
  1101.     perror("Disk_ReadLfsSuperBlock: allocating LfsSuperBlock");
  1102.     exit(FAILURE);
  1103.     }
  1104.     status = Disk_SectorRead(fileID, LFS_SUPER_BLOCK_OFFSET,
  1105.                  LFS_SUPER_BLOCK_SIZE / DEV_BYTES_PER_SECTOR,
  1106.                  (Address)superPtr);
  1107.     if (status < 0) {
  1108.     free((Address)superPtr);
  1109.     return ((LfsSuperBlock *)0);
  1110.     } else {
  1111.     if (superPtr -> hdr.magic != LFS_SUPER_BLOCK_MAGIC) {
  1112.         fprintf(stderr, "Disk_ReadLfsSuperBlock, bad magic <%x>\n",
  1113.             superPtr -> hdr.magic);
  1114.         free((Address)superPtr);
  1115.         return ((LfsSuperBlock *)0);
  1116.     } else {
  1117.         return superPtr;
  1118.     }
  1119.     }
  1120. }
  1121.  
  1122. /*
  1123.  *----------------------------------------------------------------------
  1124.  *
  1125.  * Disk_WriteLfsSuperBlock
  1126.  *
  1127.  *    Write the LFS SuperBlock to the stream.
  1128.  *
  1129.  * Results:
  1130.  *    -1 if the SuperBlock could not be written, 0 if it could.
  1131.  *
  1132.  * Side effects:
  1133.  *    Does the write.
  1134.  *
  1135.  *----------------------------------------------------------------------
  1136.  */
  1137. ReturnStatus
  1138. Disk_WriteLfsSuperBlock(fileID, superPtr)
  1139.     int         fileID;            /* Stream to raw disk */
  1140.     LfsSuperBlock    *superPtr;        /* Lfs SuperBlock */
  1141. {
  1142.     return Disk_SectorWrite(fileID, LFS_SUPER_BLOCK_OFFSET,
  1143.             LFS_SUPER_BLOCK_SIZE / DEV_BYTES_PER_SECTOR, 
  1144.             (Address)superPtr);
  1145. }
  1146.  
  1147. /*
  1148.  *----------------------------------------------------------------------
  1149.  *
  1150.  * Disk_ReadLfsCheckPointHdr
  1151.  *
  1152.  *    Read the current checkpoint region in its entirety.  The caller
  1153.  *      can choose which checkpoint region to read by specifying 
  1154.  *      either `0' or `1' in the area argument (any other value
  1155.  *      will return the current checkpoint).
  1156.  *
  1157.  * Results:
  1158.  *    A pointer to the header of the checkpoint region upon
  1159.  *      success, NULL otherwise.  Using the header, the other
  1160.  *      sections of the checkpoint can be reached.
  1161.  *
  1162.  * Side effects:
  1163.  *    Memory allocation.
  1164.  *
  1165.  *----------------------------------------------------------------------
  1166.  */
  1167. LfsCheckPointHdr *
  1168. Disk_ReadLfsCheckPointHdr(fileID, labelPtr, areaPtr)
  1169.     int        fileID;
  1170.     Disk_Label *labelPtr;
  1171.     int        *areaPtr;       /* return value specifying checkpoint area */
  1172. {
  1173.     LfsSuperBlock        *superPtr;
  1174.     LfsCheckPointHdr     *checkPointHdrPtr;
  1175.     LfsCheckPointTrailer *trailerPtr;
  1176.     int                  checkPointOffset, checkPointSize;
  1177.     int                  numSectors;
  1178.     char                 *bufferPtr;
  1179.     unsigned int         stamp0, stamp1;
  1180.     int                  status;
  1181.     
  1182.     superPtr = Disk_ReadLfsSuperBlock(fileID, labelPtr);
  1183.     if (superPtr == NULL) {
  1184.     return ((LfsCheckPointHdr *)NULL);
  1185.     }
  1186.     /*
  1187.      * Examine the two checkpoint areas to locate the checkpoint area with the
  1188.      * newest timestamp.
  1189.      */
  1190.     numSectors = ((sizeof(LfsCheckPointHdr) + DEV_BYTES_PER_SECTOR) - 1) /
  1191.              DEV_BYTES_PER_SECTOR;
  1192.     bufferPtr = (char *)malloc(numSectors * DEV_BYTES_PER_SECTOR);
  1193.     if (bufferPtr == NULL) {
  1194.     perror("Disk_ReadLfsCheckPointHdr: allocating LfsCheckPointHdr");
  1195.     exit(FAILURE);
  1196.     }
  1197.     /*
  1198.      * Checkpoint region one.
  1199.      */
  1200.     status = Disk_SectorRead(fileID, superPtr -> hdr.checkPointOffset[0],
  1201.                  numSectors, bufferPtr);
  1202.     if (status < 0) {
  1203.     fprintf(stderr, "Disk_ReadCheckPointHdr: could not read header #0\n");
  1204.     free(bufferPtr);
  1205.     free(superPtr);
  1206.     return (LfsCheckPointHdr *)NULL;
  1207.     }
  1208.     stamp0 = ((LfsCheckPointHdr *)bufferPtr) -> timestamp;
  1209.     /*
  1210.      * Checkpoint region two.
  1211.      */
  1212.     status = Disk_SectorRead(fileID, superPtr -> hdr.checkPointOffset[1],
  1213.                  numSectors, bufferPtr);
  1214.     if (status < 0) {
  1215.     fprintf(stderr, "Disk_ReadCheckPointHdr: could not read header #1\n");
  1216.     free(bufferPtr);
  1217.     free(superPtr);
  1218.     return (LfsCheckPointHdr *)NULL;
  1219.     }
  1220.     stamp1 = ((LfsCheckPointHdr *)bufferPtr) -> timestamp;
  1221.     free(bufferPtr);
  1222.     /*
  1223.      * Get the latest checkpoint header, and return in `areaPtr' 
  1224.      * which checkpoint area the header is for. (It should be in the
  1225.      * structure, but it's not so oh well.)  If `areaPtr' 
  1226.      * specifies which area to get, then get that one.
  1227.      */
  1228.     if (areaPtr != NULL && *areaPtr == 0) {
  1229.     checkPointOffset = superPtr -> hdr.checkPointOffset[0];
  1230.     } else if (areaPtr != NULL && *areaPtr == 1) {
  1231.     checkPointOffset = superPtr -> hdr.checkPointOffset[1];
  1232.     } else if (stamp0 < stamp1) {
  1233.     if (areaPtr != NULL) {
  1234.         *areaPtr = 1;
  1235.     }
  1236.     checkPointOffset = superPtr -> hdr.checkPointOffset[1];
  1237.     } else {
  1238.     if (areaPtr != NULL) {
  1239.         *areaPtr = 0;
  1240.     }
  1241.     checkPointOffset = superPtr -> hdr.checkPointOffset[0];
  1242.     }
  1243.  
  1244.     checkPointSize = superPtr -> hdr.maxCheckPointBlocks *
  1245.                  superPtr -> hdr.blockSize;
  1246.     checkPointHdrPtr = (LfsCheckPointHdr *)malloc(checkPointSize);
  1247.     if (checkPointHdrPtr == NULL) {
  1248.     perror("allocating buffer for a full Lfs CheckPoint");
  1249.     exit(FAILURE);
  1250.     }
  1251.     status = Disk_SectorRead(fileID, checkPointOffset, 
  1252.                  superPtr -> hdr.maxCheckPointBlocks,
  1253.                  (Address)checkPointHdrPtr);
  1254.     free(superPtr);
  1255.     if (status < 0) {
  1256.     fprintf(stderr, "cannot read the entire checkpoint region");
  1257.     free(checkPointHdrPtr);
  1258.     return (LfsCheckPointHdr *)NULL;
  1259.     }
  1260.     /*
  1261.      * if we cannot find a valid trailer, then the checkpoint is not valid
  1262.      */
  1263.     trailerPtr = Disk_LfsCheckPointTrailer(checkPointHdrPtr);
  1264.     if (trailerPtr == NULL) {
  1265.     free(checkPointHdrPtr);
  1266.     return (LfsCheckPointHdr *)NULL;
  1267.     }
  1268.     return checkPointHdrPtr;
  1269. }
  1270.  
  1271. /*
  1272.  *----------------------------------------------------------------------
  1273.  *
  1274.  * Disk_WriteLfsCheckPointHdr
  1275.  *
  1276.  *    Write the checkpoint header only.
  1277.  *
  1278.  * Results:
  1279.  *    0 if the header was successfully written, -1 if it wasn't.
  1280.  *
  1281.  * Side effects:
  1282.  *    Disk write.
  1283.  *
  1284.  *----------------------------------------------------------------------
  1285.  */
  1286. ReturnStatus
  1287. Disk_WriteLfsCheckPointHdr(fileID, headerPtr, area, labelPtr)
  1288.     int              fileID;
  1289.     LfsCheckPointHdr *headerPtr;
  1290.     int              area;   /* which checkpoint area this is: 0 or 1 */
  1291.     Disk_Label       *labelPtr;
  1292. {
  1293.     LfsSuperBlock *superPtr;
  1294.     int           numSectors;
  1295.     int           status;
  1296.  
  1297.     if (area < 0 || area > 1) {
  1298.     return -1;
  1299.     }
  1300.     superPtr = Disk_ReadLfsSuperBlock(fileID, labelPtr);
  1301.     if (superPtr == NULL) {
  1302.     return -1;
  1303.     }
  1304.     numSectors = ((sizeof(LfsCheckPointHdr) + DEV_BYTES_PER_SECTOR) - 1) /
  1305.              DEV_BYTES_PER_SECTOR;
  1306.     status = Disk_SectorWrite(fileID, superPtr->hdr.checkPointOffset[area],
  1307.                  numSectors, (Address)headerPtr);
  1308.     return status;
  1309. }
  1310.  
  1311. /*
  1312.  *----------------------------------------------------------------------
  1313.  *
  1314.  * Disk_WriteLfsCheckPointArea
  1315.  *
  1316.  *    Write the checkpoint area, headed by an LfsCheckPointHdr,
  1317.  *      to disk.  Such an area is returned from Disk_ReadLfsCheckPointHdr().
  1318.  *      Note: this does not constitute an update.
  1319.  *
  1320.  * Results:
  1321.  *    0 if the area was successfully written, -1 if it wasn't.
  1322.  *
  1323.  * Side effects:
  1324.  *    Disk write.
  1325.  *
  1326.  *----------------------------------------------------------------------
  1327.  */
  1328. ReturnStatus
  1329. Disk_WriteLfsCheckPointArea(fileID, areaPtr, area, labelPtr)
  1330.     int              fileID;
  1331.     LfsCheckPointHdr *areaPtr;
  1332.     int              area;  /* which checkpoint area this is: 0 or 1 */
  1333.     Disk_Label       *labelPtr;
  1334. {
  1335.     LfsSuperBlock *superPtr;
  1336.     int           status;
  1337.  
  1338.     if (area < 0 || area > 1) {
  1339.     return -1;
  1340.     }
  1341.     superPtr = Disk_ReadLfsSuperBlock(fileID, labelPtr);
  1342.     if (superPtr == NULL) {
  1343.     return -1;
  1344.     }
  1345.     status = Disk_SectorWrite(fileID, 
  1346.                   superPtr->hdr.checkPointOffset[area],
  1347.                   superPtr->hdr.maxCheckPointBlocks,
  1348.                   (Address)areaPtr);
  1349.     return status;
  1350. }
  1351.  
  1352. /*
  1353.  *----------------------------------------------------------------------
  1354.  *
  1355.  * Disk_LfsCheckPointTrailer
  1356.  *
  1357.  *    Find the trailer of the checkpoint region using the checkpoint header
  1358.  *
  1359.  * Results:
  1360.  *    A pointer to the trailer of the checkpoint region upon
  1361.  *      success, NULL otherwise.  
  1362.  *
  1363.  *      Note: the pointer returned is a pointer into the header.
  1364.  *
  1365.  * Side effects:
  1366.  *    None.
  1367.  *
  1368.  *----------------------------------------------------------------------
  1369.  */
  1370. LfsCheckPointTrailer *
  1371. Disk_LfsCheckPointTrailer(checkPointPtr)
  1372.     LfsCheckPointHdr *checkPointPtr;
  1373. {
  1374.     char                 *bytePtr;
  1375.     LfsCheckPointTrailer *trailerPtr;
  1376.  
  1377.     bytePtr = (char *)checkPointPtr;
  1378.     trailerPtr = (checkPointPtr == NULL) ? (LfsCheckPointTrailer *)NULL :
  1379.     (LfsCheckPointTrailer *)(bytePtr + checkPointPtr -> size -
  1380.                  sizeof(LfsCheckPointTrailer));
  1381.     if (trailerPtr == NULL) {
  1382.     fprintf(stderr, "Lfs CheckPoint does not have a valid trailer\n");
  1383.     return (LfsCheckPointTrailer *)NULL;
  1384.     } else if (checkPointPtr -> timestamp != trailerPtr -> timestamp) {
  1385.     fprintf(stderr, "CheckPoint header timestamp <%d> does not match\n",
  1386.         checkPointPtr -> timestamp);
  1387.     fprintf(stderr, "trailer timestamp <%d>.\n", trailerPtr -> timestamp);
  1388.     return (LfsCheckPointTrailer *)NULL;
  1389.     }
  1390.     return trailerPtr;
  1391. }
  1392.  
  1393. /*
  1394.  *----------------------------------------------------------------------
  1395.  *
  1396.  * Disk_ForEachCheckPointRegion
  1397.  *
  1398.  *    For every checkpoint region found in the checkpoint header,
  1399.  *      execute the procedure argument on that region.  The procedure
  1400.  *      argument must take a LfsCheckPointRegion pointer as an
  1401.  *      argument, and have a return value of ReturnStatus.  If the
  1402.  *      procedure returns a non-zero return value, the region iteration
  1403.  *      stops and that value is returned.
  1404.  *
  1405.  * Results:
  1406.  *      0 if there were no problems and the procedure was invoked
  1407.  *        with every checkpoit region as an argument;
  1408.  *    -1 if the trailer could not be found from the header;
  1409.  *      a non-zero return value from the procedure argument.
  1410.  *
  1411.  * Side effects:
  1412.  *    None.
  1413.  *
  1414.  *----------------------------------------------------------------------
  1415.  */
  1416. ReturnStatus
  1417. Disk_ForEachCheckPointRegion(checkPointPtr, regionProc)
  1418.     LfsCheckPointHdr *checkPointPtr;
  1419.     ReturnStatus (*regionProc)();
  1420. {
  1421.     char                 *bytePtr, *maxBytePtr;
  1422.     LfsCheckPointTrailer *trailerPtr;
  1423.     LfsCheckPointRegion  *regionPtr;
  1424.     ReturnStatus status;
  1425.  
  1426.     status = 0;
  1427.     trailerPtr = Disk_LfsCheckPointTrailer(checkPointPtr);
  1428.     if (trailerPtr == NULL) {
  1429.     return -1;
  1430.     }
  1431.     bytePtr = (char *)checkPointPtr;
  1432.     bytePtr += sizeof(LfsCheckPointHdr);
  1433.     maxBytePtr = (char *)trailerPtr;
  1434.     while (bytePtr < maxBytePtr) {
  1435.     regionPtr = (LfsCheckPointRegion *)bytePtr;
  1436.     status = regionProc(regionPtr);
  1437.     if (status) {
  1438.         return status;
  1439.     }
  1440.     bytePtr += regionPtr -> size;
  1441.     }
  1442.     return 0;
  1443. }
  1444.  
  1445.  
  1446. /*
  1447.  *----------------------------------------------------------------------
  1448.  *
  1449.  * Disk_ReadLfsSegSummary --
  1450.  *
  1451.  *    Reads a segment summary block from an LFS segment. Each segment
  1452.  *    contains a linked list of summary blocks. To read the first
  1453.  *    one pass NULL as the prevSegPtr. For subsequent summary blocks
  1454.  *    pass in a pointer to the previous block.
  1455.  *
  1456.  * Results:
  1457.  *    Pointer to the segment summary block, or NULL if the indicated
  1458.  *    block doesn't exist.
  1459.  *
  1460.  * Side effects:
  1461.  *    None.
  1462.  *
  1463.  *----------------------------------------------------------------------
  1464.  */
  1465.  
  1466. LfsSegSummary    *
  1467. Disk_ReadLfsSegSummary(fileID, superPtr, segment, prevSegPtr)
  1468.     int            fileID;        /* Stream to raw disk. */
  1469.     LfsSuperBlock    *superPtr;    /* Lfs SuperBlock */
  1470.     int            segment;    /* Segment # to use. */
  1471.     LfsSegSummary    *prevSegPtr;    /* Previous segment summary block
  1472.                      * for the segment. */
  1473.  
  1474. {
  1475.     int            blockOffset = 1;
  1476.     LfsSegSummary    *segPtr;
  1477.     int            status;
  1478.     int            segOffset;
  1479.     int            blocksPerSeg;
  1480.  
  1481.     if (prevSegPtr != NULL) {
  1482.     blockOffset = prevSegPtr->nextSummaryBlock;
  1483.     }
  1484.     blocksPerSeg = superPtr->usageArray.segmentSize/DEV_BYTES_PER_SECTOR;
  1485.     segPtr = (LfsSegSummary *) malloc(DEV_BYTES_PER_SECTOR);
  1486.     segOffset = segment * blocksPerSeg + superPtr->hdr.logStartOffset;
  1487.     status = Disk_SectorRead(fileID, segOffset + blocksPerSeg - blockOffset,
  1488.         1, (Address) segPtr);
  1489.     if (status < 0) {
  1490.     free((char *) segPtr);
  1491.     return NULL;
  1492.     }
  1493.     return segPtr;
  1494. }
  1495.  
  1496.  
  1497. /*
  1498.  *----------------------------------------------------------------------
  1499.  *
  1500.  * Disk_WriteLfsSegSummary --
  1501.  *
  1502.  *    Writes a segment summary block to an LFS segment. To write the first
  1503.  *    one pass NULL as the prevSegPtr. For subsequent summary blocks
  1504.  *    pass in a pointer to the previous block.
  1505.  *
  1506.  * Results:
  1507.  *    Standard return status.
  1508.  *
  1509.  * Side effects:
  1510.  *    None.
  1511.  *
  1512.  *----------------------------------------------------------------------
  1513.  */
  1514.  
  1515. ReturnStatus
  1516. Disk_WriteLfsSegSummary(fileID, superPtr, segment, prevSegPtr, segPtr)
  1517.     int            fileID;        /* Stream to raw disk. */
  1518.     LfsSuperBlock    *superPtr;    /* Lfs SuperBlock */
  1519.     int            segment;    /* Segment # to use. */
  1520.     LfsSegSummary    *prevSegPtr;    /* Previous segment summary block
  1521.                      * for the segment. */
  1522.     LfsSegSummary    *segPtr;    /* Segment to write. */
  1523. {
  1524.     int            blockOffset = 1;
  1525.     int            status;
  1526.     int            segOffset;
  1527.     int            blocksPerSeg;
  1528.  
  1529.     if (prevSegPtr != NULL) {
  1530.     blockOffset = prevSegPtr->nextSummaryBlock;
  1531.     }
  1532.     blocksPerSeg = superPtr->usageArray.segmentSize/DEV_BYTES_PER_SECTOR;
  1533.     segOffset = segment * blocksPerSeg + superPtr->hdr.logStartOffset;
  1534.     status = Disk_SectorWrite(fileID, segOffset + blocksPerSeg - blockOffset,
  1535.         1, (Address) segPtr);
  1536.     if (status < 0) {
  1537.     return FAILURE;
  1538.     }
  1539.     return SUCCESS;
  1540. }
  1541.  
  1542.  
  1543.  
  1544.  
  1545.  
  1546. @
  1547.  
  1548.  
  1549. 1.14
  1550. log
  1551. @lfs routines. checked in by jhh
  1552. @
  1553. text
  1554. @d17 1
  1555. a17 1
  1556. static char rcsid[] = "$Header: /sprite/src/lib/disk/RCS/diskHeader.c,v 1.13 91/10/07 17:39:57 voelker Exp Locker: voelker $ SPRITE (Berkeley)";
  1557. d1349 97
  1558. @
  1559.  
  1560.  
  1561. 1.13
  1562. log
  1563. @added routines to write out a LFS checkpoint header and a LFS
  1564. checkpoint area to disk
  1565.  
  1566. @
  1567. text
  1568. @d17 1
  1569. a17 1
  1570. static char rcsid[] = "$Header: /sprite/src/lib/disk/RCS/diskHeader.c,v 1.12 91/09/23 10:47:33 voelker Exp Locker: voelker $ SPRITE (Berkeley)";
  1571. d1058 4
  1572. a1061 1
  1573.  *    Read the current checkpoint region in its entirety.
  1574. d1074 2
  1575. a1075 2
  1576. Disk_ReadLfsCheckPointHdr(fileID, labelPtr)
  1577.     int fileID;
  1578. d1077 1
  1579. d1118 1
  1580. a1118 1
  1581.     status = Disk_SectorRead(fileID, superPtr -> hdr.checkPointOffset[0],
  1582. d1121 1
  1583. a1121 1
  1584.     fprintf(stderr, "Disk_ReadCheckPointHdr: could not read header #0\n");
  1585. d1129 4
  1586. a1132 1
  1587.      * get the latest checkpoint header
  1588. d1134 8
  1589. a1141 1
  1590.     if (stamp0 < stamp1) {
  1591. d1144 3
  1592. d1149 1
  1593. @
  1594.  
  1595.  
  1596. 1.12
  1597. log
  1598. @*** empty log message ***
  1599. @
  1600. text
  1601. @d17 1
  1602. a17 1
  1603. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.11 91/09/14 15:16:47 mendel Exp Locker: voelker $ SPRITE (Berkeley)";
  1604. d885 92
  1605. d994 7
  1606. a1000 4
  1607.     LfsSuperBlock *superPtr;
  1608.     Ofs_DomainHeader *headerPtr;
  1609.  
  1610. /*    headerPtr = Disk_ReadDomainHeader(fileID, diskLabelPtr); */
  1611. d1006 9
  1612. a1014 3
  1613.     if (Disk_SectorRead(fileID, LFS_SUPER_BLOCK_OFFSET,
  1614.             LFS_SUPER_BLOCK_SIZE / DEV_BYTES_PER_SECTOR,
  1615.             (Address)superPtr) < 0) {
  1616. a1022 3
  1617.         if (headerPtr != NULL) {
  1618.         printf("found OFS but also found superblock\n");
  1619.         }
  1620. d1044 1
  1621. a1044 1
  1622. Disk_WriteLfsSuperBlock(fileID, diskLabelPtr, superPtr)
  1623. a1045 1
  1624.     Disk_Label         *diskLabelPtr;        /* Disk label */
  1625. d1075 11
  1626. a1085 8
  1627.     LfsSuperBlock      *superPtr;
  1628.     LfsCheckPointHdr   checkPointHdr[2], *checkPointHdrPtr;
  1629.     int                checkPointOffset, checkPointSize, status;
  1630.     int                numSectors;
  1631.     char               *bufferPtr;
  1632.     unsigned int       stamp0, stamp1;
  1633.  
  1634.     if ((superPtr = Disk_ReadLfsSuperBlock(fileID, NULL)) == NULL) {
  1635. a1093 2
  1636.     printf("size of header: %d\nnumSectors: %d\nbuffer size: %d\n",
  1637.        sizeof(LfsCheckPointHdr), numSectors, numSectors*DEV_BYTES_PER_SECTOR);
  1638. d1095 10
  1639. a1104 2
  1640.     if ((Disk_SectorRead(fileID, superPtr -> hdr.checkPointOffset[0],
  1641.             numSectors, bufferPtr)) < 0) {
  1642. d1106 2
  1643. d1111 6
  1644. a1116 4
  1645.     printf("size of header: %d\nnumSectors: %d\nbuffer size: %d\n",
  1646.        sizeof(LfsCheckPointHdr), numSectors, numSectors*DEV_BYTES_PER_SECTOR);
  1647.     if ((Disk_SectorRead(fileID, superPtr -> hdr.checkPointOffset[0],
  1648.             numSectors, bufferPtr)) < 0) {
  1649. d1118 2
  1650. d1123 1
  1651. a1123 32
  1652.  
  1653. /*    if (lseek(fileID, superPtr -> hdr.checkPointOffset[0], L_SET) == -1) {
  1654.     perror("seeking for the Lfs CheckPoint Header");
  1655.     exit(FAILURE);
  1656.     }
  1657.     status = read(fileID, (char *)(checkPointHdr + 0), 
  1658.           sizeof(LfsCheckPointHdr));
  1659.     if (status == -1) {
  1660.     perror("reading Lfs CheckPoint Header #0");
  1661.     exit(FAILURE);
  1662.     } else if (status != sizeof(LfsCheckPointHdr)) {
  1663.     fprintf(stderr,"Can't read checkpoint header #0.\n");
  1664.     checkPointHdr[0].timestamp = 0;
  1665.     return (LfsCheckPointHdr *)NULL;
  1666.     } */
  1667.     /*
  1668.      * Checkpoint region two.
  1669.      */
  1670. /*    if (lseek(fileID, superPtr -> hdr.checkPointOffset[1], L_SET) == -1) {
  1671.     perror("seeking for the Lfs CheckPoint Header");
  1672.     exit(FAILURE);
  1673.     }
  1674.     status = read(fileID, (char *)(checkPointHdr + 1), 
  1675.           sizeof(LfsCheckPointHdr));
  1676.     if (status == -1) {
  1677.     perror("reading Lfs CheckPoint Header #1");
  1678.     exit(FAILURE);
  1679.     } else if (status != sizeof(LfsCheckPointHdr)) {
  1680.     fprintf(stderr,"Can't read checkpoint header #1.\n");
  1681.     checkPointHdr[1].timestamp = 0;
  1682.     return (LfsCheckPointHdr *)NULL;
  1683.     } */
  1684. a1126 1
  1685. /*    if (checkPointHdr[0].timestamp < checkPointHdr[1].timestamp) { */
  1686. d1133 3
  1687. a1135 4
  1688.     superPtr -> hdr.blockSize;
  1689.     printf("check point size: %d\n", checkPointSize);
  1690.     if ((checkPointHdrPtr = (LfsCheckPointHdr *)malloc(checkPointSize)) 
  1691.     == NULL) {
  1692. d1139 5
  1693. a1143 3
  1694.     if (Disk_SectorRead(fileID, checkPointOffset, 
  1695.             superPtr -> hdr.maxCheckPointBlocks,
  1696.             (Address)checkPointHdrPtr) < 0) {
  1697. d1145 1
  1698. d1151 3
  1699. a1153 1
  1700.     if (Disk_LfsCheckPointTrailer(checkPointHdrPtr) == NULL) {
  1701. d1162 81
  1702. d1307 1
  1703. a1307 1
  1704.     ReturnStatus (*regionProc)(LfsCheckPointRegion *);
  1705. d1315 2
  1706. a1316 1
  1707.     if ((trailerPtr = Disk_LfsCheckPointTrailer(checkPointPtr)) == NULL) {
  1708. d1332 4
  1709. @
  1710.  
  1711.  
  1712. 1.11
  1713. log
  1714. @Changes to reflect the old Sprite file system name being OFS and the
  1715. addition of LFS.
  1716. @
  1717. text
  1718. @d17 1
  1719. a17 1
  1720. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.10 90/03/16 17:41:12 jhh Exp Locker: mendel $ SPRITE (Berkeley)";
  1721. d880 285
  1722. @
  1723.  
  1724.  
  1725. 1.10
  1726. log
  1727. @replaced DiskInfo abstraction with Disk_Label
  1728. @
  1729. text
  1730. @d17 1
  1731. a17 1
  1732. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.9 90/02/16 16:09:23 shirriff Exp $ SPRITE (Berkeley)";
  1733. d180 1
  1734. a180 1
  1735. Fsdm_DomainHeader *
  1736. d185 1
  1737. a185 1
  1738.     Fsdm_DomainHeader    *headerPtr;
  1739. d187 1
  1740. a187 1
  1741.     headerPtr = (Fsdm_DomainHeader *)malloc((unsigned) 
  1742. d193 1
  1743. a193 1
  1744.     return((Fsdm_DomainHeader *)0);
  1745. d195 1
  1746. a195 1
  1747.     if (headerPtr->magic != FSDM_DOMAIN_MAGIC) {
  1748. d199 1
  1749. a199 1
  1750.         return((Fsdm_DomainHeader *)0);
  1751. d225 1
  1752. a225 1
  1753.     Fsdm_DomainHeader    *headerPtr;        /* Domain header. */
  1754. d248 1
  1755. a248 1
  1756. Fsdm_SummaryInfo *
  1757. d253 1
  1758. a253 1
  1759.     Fsdm_SummaryInfo *summaryPtr;
  1760. d255 1
  1761. a255 1
  1762.     summaryPtr = (Fsdm_SummaryInfo *) malloc (sizeof(Fsdm_SummaryInfo));
  1763. d259 1
  1764. a259 1
  1765.     return((Fsdm_SummaryInfo *)0);
  1766. d284 1
  1767. a284 1
  1768.     Fsdm_SummaryInfo *summaryPtr; /* Summary information */
  1769. d470 1
  1770. a470 1
  1771.         labelPtr->numDomainSectors = FSDM_NUM_DOMAIN_SECTORS;
  1772. d483 1
  1773. a483 1
  1774.         labelPtr->numDomainSectors = FSDM_NUM_DOMAIN_SECTORS;
  1775. d523 2
  1776. a524 2
  1777.     char         buffer[DEV_BYTES_PER_SECTOR * FSDM_NUM_DOMAIN_SECTORS];
  1778.     Fsdm_DomainHeader    *domainHeaderPtr = (Fsdm_DomainHeader *) buffer;
  1779. d548 1
  1780. a548 1
  1781.     labelPtr->numDomainSectors = FSDM_NUM_DOMAIN_SECTORS;
  1782. d557 1
  1783. a557 1
  1784.     if (Disk_SectorRead(fileID, i, FSDM_NUM_DOMAIN_SECTORS, buffer) < 0) {
  1785. d561 1
  1786. a561 1
  1787.     if (domainHeaderPtr->magic == FSDM_DOMAIN_MAGIC) {
  1788. @
  1789.  
  1790.  
  1791. 1.9
  1792. log
  1793. @Added Dec disk label code.
  1794. @
  1795. text
  1796. @d6 1
  1797. a6 2
  1798.  * Copyright (C) 1987 Regents of the University of California
  1799.  * All rights reserved.
  1800. d17 1
  1801. a17 1
  1802. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.8 90/01/31 17:05:19 jhh Exp Locker: shirriff $ SPRITE (Berkeley)";
  1803. d20 1
  1804. a20 1
  1805. #include "diskUtils.h"
  1806. d25 8
  1807. a41 130
  1808.  * Disk_ReadDiskInfo--
  1809.  *
  1810.  *    Read the first sector and determine the basic disk info.
  1811.  *
  1812.  * Results:
  1813.  *    A pointer to Disk_Info for the disk if could read it, 0
  1814.  *    otherwise.
  1815.  *
  1816.  * Side effects:
  1817.  *    Memory allocation.
  1818.  *
  1819.  *----------------------------------------------------------------------
  1820.  */
  1821. Disk_Info *
  1822. Disk_ReadDiskInfo(fileID, partition)
  1823.     int fileID;            /* Handle on raw disk */
  1824.     int partition;        /* Index of the partition to format */
  1825. {
  1826.     Sun_DiskLabel    *sunLabelPtr;
  1827.     Dec_DiskLabel    *decLabelPtr;
  1828.     Disk_Info        *diskInfoPtr;
  1829.     char         buffer[DEV_BYTES_PER_SECTOR * FSDM_NUM_DOMAIN_SECTORS];
  1830.     Fsdm_DomainHeader    *domainHeaderPtr = (Fsdm_DomainHeader *) buffer;
  1831.     int            i;
  1832.  
  1833.     diskInfoPtr = (Disk_Info *)malloc(sizeof(Disk_Info));
  1834.  
  1835.     sunLabelPtr = Disk_ReadSunLabel(fileID);
  1836.     if (sunLabelPtr != (Sun_DiskLabel *)0) {
  1837.     Sun_DiskMap *sunMapPtr;
  1838.     /*
  1839.      * First sector looks like a sun label.
  1840.      */
  1841.     sunMapPtr = &sunLabelPtr->map[partition];
  1842.     (void)strcpy(diskInfoPtr->asciiLabel, sunLabelPtr->asciiLabel);
  1843.     diskInfoPtr->bootSector = BOOT_SECTOR;
  1844.     diskInfoPtr->numDomainSectors = FSDM_NUM_DOMAIN_SECTORS;
  1845.     diskInfoPtr->firstCylinder = sunMapPtr->cylinder;
  1846.     diskInfoPtr->numCylinders = sunMapPtr->numBlocks /
  1847.         (sunLabelPtr->numHeads * sunLabelPtr->numSectors);
  1848.     diskInfoPtr->numHeads = sunLabelPtr->numHeads;
  1849.     diskInfoPtr->numSectors = sunLabelPtr->numSectors;
  1850.     for (i = BOOT_SECTOR + 1; 
  1851.          i < FSDM_MAX_BOOT_SECTORS + 3; 
  1852.          i+= FSDM_BOOT_SECTOR_INC) {
  1853.         if (Disk_SectorRead(fileID, i, FSDM_NUM_DOMAIN_SECTORS, buffer) < 0) {
  1854.         fprintf(stderr, "Can't read sector %d.\n", i);
  1855.         return((Disk_Info *)0);
  1856.         }
  1857.         if (domainHeaderPtr->magic == FSDM_DOMAIN_MAGIC) {
  1858.         diskInfoPtr->summarySector = i - 1;
  1859.         diskInfoPtr->domainSector = i;
  1860.         diskInfoPtr->numBootSectors = diskInfoPtr->summarySector - 1;
  1861.         break;
  1862.         }
  1863.     }
  1864.     if (i >= FSDM_MAX_BOOT_SECTORS + 3) {
  1865.         diskInfoPtr->summarySector = -1;
  1866.         diskInfoPtr->domainSector = -1;
  1867.         diskInfoPtr->numBootSectors = -1;
  1868.     }
  1869.     } else {
  1870.     Fsdm_DiskHeader *diskHeaderPtr;
  1871.     Fsdm_DiskPartition *mapPtr;
  1872.  
  1873.     /*
  1874.      * Not a sun label, try a dec or sprite label.
  1875.      */
  1876.     diskHeaderPtr = Disk_ReadDiskHeader(fileID);
  1877.     if (diskHeaderPtr != (Fsdm_DiskHeader *)0) {
  1878.         mapPtr = &diskHeaderPtr->map[partition];
  1879.         (void)strcpy(diskInfoPtr->asciiLabel, diskHeaderPtr->asciiLabel);
  1880.         diskInfoPtr->bootSector = diskHeaderPtr->bootSector;
  1881.         diskInfoPtr->numBootSectors = diskHeaderPtr->numBootSectors;
  1882.         diskInfoPtr->summarySector = diskHeaderPtr->summarySector;
  1883.         diskInfoPtr->domainSector = diskHeaderPtr->domainSector;
  1884.         diskInfoPtr->numDomainSectors = diskHeaderPtr->numDomainSectors;
  1885.         diskInfoPtr->firstCylinder = mapPtr->firstCylinder;
  1886.         diskInfoPtr->numCylinders = mapPtr->numCylinders;
  1887.         diskInfoPtr->numHeads = diskHeaderPtr->numHeads;
  1888.         diskInfoPtr->numSectors = diskHeaderPtr->numSectors;
  1889.     } else {
  1890.         decLabelPtr = Disk_ReadDecLabel(fileID);
  1891.         if (decLabelPtr != (Dec_DiskLabel *)0) {
  1892.         Dec_DiskMap *decMapPtr;
  1893.         int    cylLength;
  1894.         /*
  1895.          * Sector looks like a dec label.
  1896.          */
  1897.         decMapPtr = &decLabelPtr->map[partition];
  1898.         if (decLabelPtr->numHeads<0) {
  1899.             fprintf(stderr,"Warning: not a Sprite-modified %s",
  1900.             "Dec label.  Using defaults for #heads, etc.\n");
  1901.             diskInfoPtr->numHeads = 4;
  1902.             diskInfoPtr->numSectors = 33;
  1903.             diskInfoPtr->summarySector = -1;
  1904.             diskInfoPtr->domainSector = -1;
  1905.             diskInfoPtr->numBootSectors = -1;
  1906.         } else {
  1907.             diskInfoPtr->numHeads = decLabelPtr->numHeads;
  1908.             diskInfoPtr->numSectors = decLabelPtr->numSectors;
  1909.         }
  1910.         cylLength = diskInfoPtr->numHeads * diskInfoPtr->numSectors *
  1911.             DEV_BYTES_PER_SECTOR;
  1912.         if (decMapPtr->numBytes % cylLength != 0 ||
  1913.             decMapPtr->offsetBytes % cylLength != 0) {
  1914.             fprintf(stderr,"Warning: dec disk partition is not %s!\n",
  1915.                 "a multiple of cylinder size");
  1916.         }
  1917.         (void)strcpy(diskInfoPtr->asciiLabel, decLabelPtr->asciiLabel);
  1918.         diskInfoPtr->firstCylinder = decMapPtr->offsetBytes / cylLength;
  1919.         diskInfoPtr->numCylinders = decMapPtr->numBytes / cylLength;
  1920.         diskInfoPtr->bootSector = decLabelPtr->bootSector;
  1921.         diskInfoPtr->numDomainSectors = decLabelPtr->numDomainSectors;
  1922.         diskInfoPtr->numBootSectors = decLabelPtr->numBootSectors;
  1923.         diskInfoPtr->summarySector = decLabelPtr->summarySector;
  1924.         diskInfoPtr->domainSector = decLabelPtr->domainSector;
  1925.         diskInfoPtr->numDomainSectors = decLabelPtr->numDomainSectors;
  1926.         } else {
  1927.         fprintf(stderr, "Not a Sun, Sprite, or Dec label\n");
  1928.         return((Disk_Info *)0);
  1929.         }
  1930.     }
  1931.     }
  1932.     return(diskInfoPtr);
  1933. }
  1934.  
  1935. /*
  1936.  *----------------------------------------------------------------------
  1937.  *
  1938. d133 1
  1939. d169 1
  1940. d181 1
  1941. a181 1
  1942. Disk_ReadDomainHeader(fileID, diskInfoPtr)
  1943. d183 1
  1944. a183 1
  1945.     Disk_Info *diskInfoPtr;    /* Basic disk information */
  1946. d187 2
  1947. a188 2
  1948.     headerPtr = (Fsdm_DomainHeader *)malloc(diskInfoPtr->numDomainSectors *
  1949.                      DEV_BYTES_PER_SECTOR);
  1950. d190 2
  1951. a191 2
  1952.     if (Disk_SectorRead(fileID, diskInfoPtr->domainSector,
  1953.                     diskInfoPtr->numDomainSectors,
  1954. d209 26
  1955. d249 1
  1956. a249 1
  1957. Disk_ReadSummaryInfo(fileID, diskInfoPtr)
  1958. d251 1
  1959. a251 1
  1960.     Disk_Info *diskInfoPtr;    /* Basic disk information */
  1961. d257 2
  1962. a258 2
  1963.     if (Disk_SectorRead(fileID, diskInfoPtr->summarySector, 1,
  1964.                     (Address)summaryPtr) < 0) {
  1965. d273 1
  1966. a273 1
  1967.  *    0 if write succeeded, -1 otherwise
  1968. d280 2
  1969. a281 2
  1970. int
  1971. Disk_WriteSummaryInfo(fileID, diskInfoPtr, summaryPtr)
  1972. d283 1
  1973. a283 1
  1974.     Disk_Info *diskInfoPtr;    /* Basic disk information */
  1975. d286 572
  1976. d859 21
  1977. a879 2
  1978.     return Disk_SectorWrite(fileID, diskInfoPtr->summarySector, 1,
  1979.                     (Address)summaryPtr);
  1980. d881 1
  1981. @
  1982.  
  1983.  
  1984. 1.8
  1985. log
  1986. @added routines to read and write summary info
  1987. @
  1988. text
  1989. @d18 1
  1990. a18 1
  1991. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.7 89/09/25 12:32:34 jhh Exp Locker: jhh $ SPRITE (Berkeley)";
  1992. d54 1
  1993. d101 1
  1994. a101 1
  1995.      * Not a sun label, try a sprite label.
  1996. d104 53
  1997. a156 3
  1998.     if (diskHeaderPtr == (Fsdm_DiskHeader *)0) {
  1999.         fprintf(stderr, "Neither Sun nor Sprite label\n");
  2000.         return((Disk_Info *)0);
  2001. a157 11
  2002.     mapPtr = &diskHeaderPtr->map[partition];
  2003.     (void)strcpy(diskInfoPtr->asciiLabel, diskHeaderPtr->asciiLabel);
  2004.     diskInfoPtr->bootSector = diskHeaderPtr->bootSector;
  2005.     diskInfoPtr->numBootSectors = diskHeaderPtr->numBootSectors;
  2006.     diskInfoPtr->summarySector = diskHeaderPtr->summarySector;
  2007.     diskInfoPtr->domainSector = diskHeaderPtr->domainSector;
  2008.     diskInfoPtr->numDomainSectors = diskHeaderPtr->numDomainSectors;
  2009.     diskInfoPtr->firstCylinder = mapPtr->firstCylinder;
  2010.     diskInfoPtr->numCylinders = mapPtr->numCylinders;
  2011.     diskInfoPtr->numHeads = diskHeaderPtr->numHeads;
  2012.     diskInfoPtr->numSectors = diskHeaderPtr->numSectors;
  2013. d161 27
  2014. d189 1
  2015. d191 21
  2016. d239 1
  2017. d244 1
  2018. @
  2019.  
  2020.  
  2021. 1.7
  2022. log
  2023. @Conforms to new fs module structure
  2024. @
  2025. text
  2026. @d18 1
  2027. a18 1
  2028. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.6 89/09/12 11:51:14 jhh Exp Locker: jhh $ SPRITE (Berkeley)";
  2029. d235 55
  2030. d291 3
  2031. @
  2032.  
  2033.  
  2034. 1.6
  2035. log
  2036. @fixed bug reading partition information
  2037. .
  2038. @
  2039. text
  2040. @d18 1
  2041. a18 1
  2042. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.5 89/07/31 17:42:19 jhh Exp $ SPRITE (Berkeley)";
  2043. d55 2
  2044. a56 2
  2045.     char         buffer[DEV_BYTES_PER_SECTOR * FS_NUM_DOMAIN_SECTORS];
  2046.     FsDomainHeader    *domainHeaderPtr = (FsDomainHeader *) buffer;
  2047. d70 1
  2048. a70 1
  2049.     diskInfoPtr->numDomainSectors = FS_NUM_DOMAIN_SECTORS;
  2050. d77 3
  2051. a79 3
  2052.          i < FS_MAX_BOOT_SECTORS + 3; 
  2053.          i+= FS_BOOT_SECTOR_INC) {
  2054.         if (Disk_SectorRead(fileID, i, FS_NUM_DOMAIN_SECTORS, buffer) < 0) {
  2055. d83 1
  2056. a83 1
  2057.         if (domainHeaderPtr->magic == FS_DOMAIN_MAGIC) {
  2058. d90 1
  2059. a90 1
  2060.     if (i >= FS_MAX_BOOT_SECTORS + 3) {
  2061. d96 2
  2062. a97 2
  2063.     FsDiskHeader *diskHeaderPtr;
  2064.     FsDiskPartition *mapPtr;
  2065. d103 1
  2066. a103 1
  2067.     if (diskHeaderPtr == (FsDiskHeader *)0) {
  2068. d175 1
  2069. a175 1
  2070. FsDiskHeader *
  2071. d180 1
  2072. a180 1
  2073.     FsDiskHeader    *diskHeaderPtr;
  2074. d185 1
  2075. a185 1
  2076.     return((FsDiskHeader *)0);
  2077. d187 3
  2078. a189 3
  2079.     diskHeaderPtr = (FsDiskHeader *)buffer;
  2080.     if (diskHeaderPtr->magic != FS_DISK_MAGIC) {
  2081.         return((FsDiskHeader *)0);
  2082. d210 1
  2083. a210 1
  2084. FsDomainHeader *
  2085. d215 1
  2086. a215 1
  2087.     FsDomainHeader    *headerPtr;
  2088. d217 1
  2089. a217 1
  2090.     headerPtr = (FsDomainHeader *)malloc(diskInfoPtr->numDomainSectors *
  2091. d223 1
  2092. a223 1
  2093.     return((FsDomainHeader *)0);
  2094. d225 1
  2095. a225 1
  2096.     if (headerPtr->magic != FS_DOMAIN_MAGIC) {
  2097. d229 1
  2098. a229 1
  2099.         return((FsDomainHeader *)0);
  2100. @
  2101.  
  2102.  
  2103. 1.5
  2104. log
  2105. @variable number of boot sectors
  2106. @
  2107. text
  2108. @d18 1
  2109. a18 1
  2110. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.4 89/02/09 11:14:19 brent Exp $ SPRITE (Berkeley)";
  2111. d107 1
  2112. a107 1
  2113.     mapPtr = &diskHeaderPtr->map[diskHeaderPtr->partition];
  2114. @
  2115.  
  2116.  
  2117. 1.4
  2118. log
  2119. @Added Disk_ReadDomainHeader
  2120. @
  2121. text
  2122. @d18 1
  2123. a18 1
  2124. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.3 88/10/27 15:30:43 nelson Exp $ SPRITE (Berkeley)";
  2125. d24 1
  2126. a27 1
  2127.  * NUM_BOOT_SECTORS    The number of boot sectors.
  2128. d30 1
  2129. a30 2
  2130. #define    NUM_BOOT_SECTORS    15
  2131.  
  2132. d35 1
  2133. a35 1
  2134.  * Disk_ReadDiskInfo --
  2135. d55 3
  2136. a69 3
  2137.     diskInfoPtr->numBootSectors = NUM_BOOT_SECTORS;
  2138.     diskInfoPtr->summarySector = SUN_SUMMARY_SECTOR;
  2139.     diskInfoPtr->domainSector = SUN_DOMAIN_SECTOR;
  2140. d76 19
  2141. d122 1
  2142. a214 1
  2143.     Address        buffer;
  2144. @
  2145.  
  2146.  
  2147. 1.3
  2148. log
  2149. @Ported to the new C library.
  2150. @
  2151. text
  2152. @d18 1
  2153. a18 1
  2154. static char rcsid[] = "$Header: diskHeader.c,v 1.2 88/07/19 11:54:04 douglis Exp $ SPRITE (Berkeley)";
  2155. d173 41
  2156. @
  2157.  
  2158.  
  2159. 1.2
  2160. log
  2161. @removed some lint.
  2162. @
  2163. text
  2164. @d18 1
  2165. a18 1
  2166. static char rcsid[] = "$Header: diskHeader.c,v 1.1 88/06/02 12:54:00 brent Exp $ SPRITE (Berkeley)";
  2167. a20 2
  2168. #include "sprite.h"
  2169. #include "io.h"
  2170. d22 2
  2171. a23 3
  2172. #include "mem.h"
  2173. #include "byte.h"
  2174. #include "string.h"
  2175. d25 7
  2176. d36 2
  2177. a37 1
  2178.  * ReadBasicDiskInfo --
  2179. d41 2
  2180. a42 1
  2181.  *    A pointer to BasicDisInfo for the disk.
  2182. d49 2
  2183. a50 2
  2184. BasicDiskInfo *
  2185. ReadBasicDiskInfo(fileID, partition)
  2186. d54 2
  2187. a55 2
  2188.     Sun_DiskLabel *sunLabelPtr;
  2189.     BasicDiskInfo *diskInfoPtr;
  2190. d57 1
  2191. a57 1
  2192.     diskInfoPtr = (BasicDiskInfo *)Mem_Alloc(sizeof(BasicDiskInfo));
  2193. d59 1
  2194. a59 1
  2195.     sunLabelPtr = ReadSunLabel(fileID);
  2196. d66 3
  2197. a68 3
  2198.     (void)String_Copy(sunLabelPtr->asciiLabel, diskInfoPtr->asciiLabel);
  2199.     diskInfoPtr->bootSector = SUN_BOOT_SECTOR;
  2200.     diskInfoPtr->numBootSectors = FS_NUM_BOOT_SECTORS;
  2201. d84 1
  2202. a84 1
  2203.     diskHeaderPtr = ReadDiskHeader(fileID);
  2204. d86 2
  2205. a87 2
  2206.         Io_PrintStream(io_StdErr, "Neither Sun nor Sprite label\n");
  2207.         return((BasicDiskInfo *)0);
  2208. d90 1
  2209. a90 1
  2210.     (void)String_Copy(diskHeaderPtr->asciiLabel, diskInfoPtr->asciiLabel);
  2211. d103 1
  2212. d108 1
  2213. a108 1
  2214.  * ReadSunLabel --
  2215. d114 1
  2216. a114 1
  2217.  *    A pointer to the super block data.
  2218. d122 1
  2219. a122 1
  2220. ReadSunLabel(fileID)
  2221. d125 2
  2222. a126 3
  2223.     ReturnStatus status;
  2224.     Address buffer;
  2225.     Sun_DiskLabel *sunLabelPtr;
  2226. d128 1
  2227. a128 1
  2228.     buffer = (Address) Mem_Alloc(DEV_BYTES_PER_SECTOR);
  2229. d130 1
  2230. a130 2
  2231.     status = SectorRead(fileID, 0, 1, buffer);
  2232.     if (status != SUCCESS) {
  2233. d145 1
  2234. a145 1
  2235.  * ReadDiskHeader --
  2236. d149 1
  2237. a149 1
  2238.  *    A pointer to the super block data.
  2239. d157 1
  2240. a157 1
  2241. ReadDiskHeader(openFileID)
  2242. d160 2
  2243. a161 3
  2244.     ReturnStatus status;
  2245.     Address buffer;
  2246.     FsDiskHeader *diskHeaderPtr;
  2247. d163 1
  2248. a163 1
  2249.     buffer = (Address) Mem_Alloc(DEV_BYTES_PER_SECTOR);
  2250. d165 1
  2251. a165 2
  2252.     status = SectorRead(openFileID, 0, 1, buffer);
  2253.     if (status != SUCCESS) {
  2254. @
  2255.  
  2256.  
  2257. 1.1
  2258. log
  2259. @Initial revision
  2260. @
  2261. text
  2262. @d18 1
  2263. a18 1
  2264. static char rcsid[] = "$Header: fsDiskUtils.c,v 1.4 87/06/02 11:20:32 nelson Exp $ SPRITE (Berkeley)";
  2265. d26 1
  2266. d60 1
  2267. a60 1
  2268.     String_Copy(sunLabelPtr->asciiLabel, diskInfoPtr->asciiLabel);
  2269. d84 1
  2270. a84 1
  2271.     String_Copy(diskHeaderPtr->asciiLabel, diskInfoPtr->asciiLabel);
  2272. @
  2273.